package com.onec.service.manage.service.impl;

import com.onec.service.api.enums.StatusEnum;
import com.onec.service.manage.dao.UserInfoMapper;
import com.onec.service.manage.dto.req.UserInfoReq;
import com.onec.service.manage.entity.UserInfo;
import com.onec.service.manage.service.UserManageService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;


/**
 * 用户管理实现
 *
 * @author ONEC
 */
@Service
public class UserManageServiceImpl implements UserManageService {

    @Resource
    private UserInfoMapper userInfoMapper;

    @Override
    public List<UserInfo> userList() {
        //return userInfoMapper.findAll();
        return null;
    }

    @Override
    public UserInfo selectById(Long userId) {
        UserInfo userInfo = userInfoMapper.selectUserById(userId, StatusEnum.SUCCESS);
//        if (optional.isPresent()) {
//            return optional.get();
//        }
        //throw new ManageException(20003, "用户信息查询失败");
        return userInfo;
    }

    /**
     * jpa 修改：通过设置save 主键保存
     *
     * @param userInfoDto
     */
    @RequiresPermissions("PN0001")
    @Override
    public void updateUserId(UserInfoReq userInfoDto) {
        UserInfo userInfo = userInfoMapper.selectUserById(userInfoDto.getId(), StatusEnum.SUCCESS);
        if (Optional.ofNullable(userInfo).isPresent()) {

            // userRepository.saveAndFlush(updateInfo);
        } else {
            // throw new ManageException(1000, "請求商戶訂單有無");
        }
    }

    /**
     * 添加运营管理人员,以及相关的角色
     *
     * @param userInfoDto
     */
    @RequiresPermissions("PN0001")
    @Override
    public void addUser(UserInfoReq userInfoDto) {
        UserInfo userInfo = new UserInfo();
        userInfoMapper.insertUserInfo(userInfo);
    }
}
